package cn.com.zcode.exam.control;

import java.net.URISyntaxException;
import java.util.Collection;
import java.util.List;

import cn.com.zcode.core.control.BaseActionSupport;
import cn.com.zcode.core.util.ObjectUtil;
import cn.com.zcode.exam.model.Paper;
import cn.com.zcode.exam.model.Question;
import cn.com.zcode.exam.model.StaticInfo;
import cn.com.zcode.exam.service.ExamService;
import cn.com.zcode.user.model.User;

import com.opensymphony.xwork2.ActionContext;

public class PaperAction extends BaseActionSupport<ExamService> {
	private static final long serialVersionUID = 1L;
	private String fileName;
	private String keyword;
	private boolean editQuestions;

	private Paper paper;
	private Question question;
	private List<Paper> papers;
	private List<StaticInfo> staticInfos;
	private List<Question> questions ;
	
	
	public PaperAction() {
		super();
	}

	public String findPapers() {
		if (paper != null) {
			User user = this.getSessionAttribute(User.SESSION_USER, User.class);

			papers = this.service.findPapers(paper.getType(),user.getId());
		}
		return SUCCESS;
	}

	public String paperStore(){
		
		return INPUT;
	}
	
	public String getPaperInfo() {
		staticInfos = this.service.getPaperInfo(paper.getType());
		return SUCCESS;
	}

	public List<StaticInfo> getStaticInfos() {
		return staticInfos;
	}

	public void setStaticInfos(List<StaticInfo> staticInfos) {
		this.staticInfos = staticInfos;
	}

	public String searchQuestions() {
		if (keyword == null) {
			return INPUT;
		} else {
			User user = this.getSessionAttribute(User.SESSION_USER, User.class);
			List<Question> list = this.service.searchQuestions(keyword,user.getId());
			this.addAttribute("results", list);

		}
		return SUCCESS;
	}

	 
	
	 
	
	
	
	@Deprecated
	@SuppressWarnings("unchecked")
	public String addPaper() {
		if (paper == null) {
			return INPUT;
		} else {
			Collection<Question> qs = null;
			if (paper.getId()==null) {
				User user = this.getSessionAttribute("user", User.class);
				paper.setCreater(user);
				service.addPaper(paper);

			}
			if (editQuestions) {
				paper = service.getPaper(paper.getId());
				qs = paper.getQuestions();

			} else {
				if (!(fileName = fileName.trim()).isEmpty()) {
					try {
						String path = this.getClass().getClassLoader().getResource(fileName).toURI().getPath();
						qs = ObjectUtil.readObject(path, List.class);
						ActionContext.getContext().getApplication().put("questions", qs);
					} catch (URISyntaxException e) {
						LOG.error("加载失败.....");
					}
				}
			}
			addAttribute("questions", qs);
		}

		return SUCCESS;
	}
	public String editQuestions() {
		String qid = question.getId();
		question = service.getQuestion(qid);
		return SUCCESS;
	}
	public String updateQuestion() {
		if(question !=null){
			this.service.updateQuestion(question);
		} 
		this.addAttribute("updated", true);
		return SUCCESS;
	}

	public void validateAddPaper() {
		super.validate();
		if (paper != null&&paper.getId()==null) {
			User user = this.getSessionAttribute("user", User.class);
			Paper rePaper = service.getPaper(paper.getType(), paper.getTitle(), paper.getState(), user.getId());
			if (rePaper != null) {
				this.addFieldError("paper.title", "试卷名称重复");
			}
		}
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


	public String addQuestion() {
		User user = this.getSessionAttribute(User.SESSION_USER, User.class);
		if(question!=null){
			Paper paper = question.getPaper();
			paper.setCreater(user);
			this.service.addQuestion(question);
			return SUCCESS;
		}else{
			papers = this.service.findEditablePaper(paper.getType(), user);
			
		}

		return INPUT;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public List<Paper> getPapers() {
		return papers;
	}

	public void setPapers(List<Paper> papers) {
		this.papers = papers;
	}

	public boolean isEditQuestions() {
		return editQuestions;
	}

	public void setEditQuestions(boolean editQuestions) {
		this.editQuestions = editQuestions;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> qustions) {
		this.questions = qustions;
	}


}
